//
//  educationView.m
//  HappyStore
//
//  Created by 点点充 on 16/1/27.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "educationView.h"

@interface educationView ()<UIGestureRecognizerDelegate>
{
    BOOL selectStartAndEndDate;
    
    NSString *strStartDate;
    NSString *strEndDate;
    
    UIButton *btnAddLogo;
    UIImage *imgSelected;
    
}

@property (nonatomic, strong) UITextField *tf1;
@property (nonatomic, strong) UITextField *tf2;
@property (nonatomic, strong) UILabel *labStartTime;
@property (nonatomic, strong) UILabel *labEndTime;
@property (strong, nonatomic) NSArray *educationalAry;//学历
@property (nonatomic, strong) UIViewController *mainVC;

@end


@implementation educationView
- (NSArray *)educationalAry{
    if (!_educationalAry) {
        _educationalAry = @[@"初中",@"高中",@"专科",@"大学",@"硕士",@"博士"];
    }
    return _educationalAry;
}
@synthesize tf1,tf2,labStartTime,labEndTime;

+ (educationView *)sharedSelectedAddress
{
    static educationView *_sharedService = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedService = [[educationView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _sharedService.backgroundColor = [[UIColor colorWithWhite:0.6 alpha:1.0f] colorWithAlphaComponent:0.7];
    });
    return _sharedService;
}

#pragma mark - 添加教育经历
- (void)addEduExpWith:(UIViewController *)vc
{
    self.mainVC = vc;
    self.hidden = NO;
    //加入手势来取消键盘
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(dismissView)];
    tapRecognizer.cancelsTouchesInView = NO;
    tapRecognizer.delegate = self;
    [self addGestureRecognizer:tapRecognizer];
    
    UIView *viewBack = [[UIView alloc] initWithFrame:(CGRect){20,(self.frame.size.height-330)/2,280,330}];
    viewBack.backgroundColor = [UIColor whiteColor];
    viewBack.layer.masksToBounds = YES;
    viewBack.layer.cornerRadius = 10.f;
    [self addSubview:viewBack];
    
    btnAddLogo = [UIButton buttonWithType:UIButtonTypeCustom];
    btnAddLogo.frame = (CGRect){100,10,80,80};
    btnAddLogo.layer.masksToBounds = YES;
    btnAddLogo.layer.cornerRadius = 5.f;
    [btnAddLogo addTarget:self action:@selector(btnClickSelecteImg) forControlEvents:UIControlEventTouchUpInside];
    [btnAddLogo setImage:[UIImage imageNamed:@"btn_myD_addSchool"] forState:UIControlStateNormal];
    [viewBack addSubview:btnAddLogo];
    
    NSArray *aryTitle = @[@"专业",@"学校",@"时间"];
    for (int i = 0; i<aryTitle.count; i++) {
        UILabel *labTitle = [[UILabel alloc] initWithFrame:(CGRect){10,105+40*i,30,20}];
        labTitle.text = aryTitle[i];
        labTitle.font = [UIFont systemFontOfSize:14.5f];
        labTitle.textColor = [UIColor colorWithRed:89/255.f green:87/255.f blue:87/255.f alpha:1.f];
        [viewBack addSubview:labTitle];
    }
    
    tf1 = [[UITextField alloc] initWithFrame:(CGRect){50,100,210,30}];
    tf1.placeholder = @"请输入您的专业名称";
    tf1.layer.masksToBounds = YES;
    tf1.layer.cornerRadius = 5.f;
    tf1.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    tf1.layer.borderWidth = 0.5f;
    tf1.font = [UIFont systemFontOfSize:12.5f];
    tf1.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    [viewBack addSubview:tf1];
    
    tf2 = [[UITextField alloc] initWithFrame:(CGRect){50,140,210,30}];
    tf2.placeholder = @"请输入您的学校名称";
    tf2.layer.masksToBounds = YES;
    tf2.layer.cornerRadius = 5.f;
    tf2.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    tf2.layer.borderWidth = 0.5f;
    tf2.font = [UIFont systemFontOfSize:12.5f];
    tf2.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    [viewBack addSubview:tf2];
    
    labStartTime = [[UILabel alloc] initWithFrame:(CGRect){50,180,210,30}];
    labStartTime.layer.masksToBounds = YES;
    labStartTime.layer.cornerRadius = 5.0f;
    labStartTime.text = @"请选择教育开始时间";
    labStartTime.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    labStartTime.layer.borderWidth = 0.5f;
    labStartTime.font = [UIFont systemFontOfSize:12.5f];
    labStartTime.textColor = [UIColor colorWithWhite:0.75 alpha:1.f];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"re_project_right"] forState:UIControlStateNormal];
    btn1.frame = (CGRect){50,180.5,210,29.5};
    
    btn1.imageEdgeInsets = UIEdgeInsetsMake(0, 180, 0, 0);
    [btn1 addTarget:self action:@selector(btnClickStartDate:) forControlEvents:UIControlEventTouchUpInside];
    [viewBack addSubview:labStartTime];
    [viewBack addSubview:btn1];
    
    
    labEndTime = [[UILabel alloc] initWithFrame:(CGRect){50,220,210,30}];
    labEndTime.layer.masksToBounds = YES;
    labEndTime.layer.cornerRadius = 5.0f;
    labEndTime.text = @"请选择教育结束时间";
    labEndTime.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
    labEndTime.layer.borderWidth = 0.5f;
    labEndTime.font = [UIFont systemFontOfSize:12.5f];
    labEndTime.textColor = [UIColor colorWithWhite:0.75 alpha:1.f];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"re_project_right"] forState:UIControlStateNormal];
    btn2.frame = (CGRect){50,220.5,210,28.5};
    
    btn2.imageEdgeInsets = UIEdgeInsetsMake(0, 180, 0, 0);
    [btn2 addTarget:self action:@selector(btnClickEndDate:) forControlEvents:UIControlEventTouchUpInside];
    
    [viewBack addSubview:labEndTime];
    [viewBack addSubview:btn2];
    
    
    UIButton *btnEnter = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEnter.frame = (CGRect){10,270,260,40};
    btnEnter.backgroundColor = [UIColor colorWithRed:32/255.0f green:165/255.0f blue:240/255.0f alpha:1.0f];
    btnEnter.layer.masksToBounds = YES;
    btnEnter.layer.cornerRadius = 5.f;
    btnEnter.titleLabel.font = [UIFont systemFontOfSize:14.5f];
    [btnEnter setTitle:@"确定" forState:UIControlStateNormal];
    [btnEnter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnEnter addTarget:self action:@selector(btnClickOK) forControlEvents:UIControlEventTouchUpInside];
    [viewBack addSubview:btnEnter];
    
    
//    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self];
    
    [self.mainVC.view addSubview:self];
    
}

#pragma mark - 选择logo
- (void)btnClickSelecteImg
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"照片选择"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"相机",@"相册", nil];
    [actionSheet showInView:self];
    
}


#pragma mark - 选择开始时间
- (void)btnClickStartDate:(UIButton *)sender
{
    selectStartAndEndDate = YES;
    eduDatePickView *datt = [eduDatePickView sharedSelectedAddress];
    datt.delegatee = self;
    [datt btnClickDate];
}

#pragma mark - 选择结束时间
- (void)btnClickEndDate:(UIButton *)sender
{
    selectStartAndEndDate = NO;
    eduDatePickView *datt = [eduDatePickView sharedSelectedAddress];
    datt.delegatee = self;
    [datt btnClickDate];
}


- (void)seceltedDate:(NSString *)date
{
    if (selectStartAndEndDate) {
        strStartDate = date;
        labStartTime.text = date;
        labStartTime.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    }else {
        strEndDate = date;
        labEndTime.text = date;
        labEndTime.textColor = [UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f];
    }
}

- (void)dismissView
{
    self.hidden = YES;
}

#pragma mark - 选择照片
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    pickerImage.delegate = self;
    pickerImage.allowsEditing = YES;//设置可编辑
    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"相机");
            pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self.mainVC presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 1:
        {
            NSLog(@"相册");
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库
            [self.mainVC presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 2:
        {
            NSLog(@"取消");
        }
            break;
    }
    
}

//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [btnAddLogo setImage:image forState:UIControlStateNormal];
    imgSelected = image;
}

//点击cancel 调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if (self.superview!=nil) {
        if ([touch.view isKindOfClass:[UIView class]] && touch.view != self) {
            return NO;
        }
    }
    return YES;
}

- (void)btnClickOK
{
    BOOL isOk = YES;


    if (isOk==YES && tf1.text.length == 0) {
        isOk = NO;

        [self showSingleIconBubble:@"请输入专业名称"];
    }
    
    if (isOk==YES && tf2.text.length == 0) {
        isOk = NO;
        [self showSingleIconBubble:@"请输入学校名称"];
    }
    
    if (isOk && strStartDate.length == 0) {
        isOk = NO;
        [self showSingleIconBubble:@"请选择教育开始时间"];
    }
    
    if (isOk && strEndDate.length == 0) {
        isOk = NO;
        [self showSingleIconBubble:@"请选择教育结束时间"];
    }
    
    if (isOk) {
        NSString *strTime = [NSString stringWithFormat:@"%@-%@",strStartDate,strEndDate];
        [self.delegatee AddDelegateWithTime:strTime withSchoolName:tf2.text withSchoolStudy:tf1.text withLogo:imgSelected];
        self.hidden = YES;
    }
    
    
}


@end
